Slack

If you want to send message using incoming webhook, you can use webhook.

Parameters

The Slack notification service configuration includes following settings:

Configuration

  1. Create Slack Application using https://api.slack.com/apps?new\_app=1 1
  2. Once application is created navigate to Enter OAuth & Permissions 2
  3. Click Permissions under Add features and functionality section and add chat:write scope. To use the optional username and icon overrides in the Slack notification service also add the chat:write.customize scope. 3
  4. Scroll back to the top, click ‘Install App to Workspace’ button and confirm the installation. 4
  5. Once installation is completed copy the OAuth token. 5

  6. Create a public or private channel, for this example my_channel

  7. Invite your slack bot to this channel otherwise slack bot won’t be able to deliver notifications to this channel

  8. Store Oauth access token in argocd-notifications-secret secret
  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: <secret-name>
  5. stringData:
  6. slack-token: <Oauth-access-token>
  1. Define service type slack in data section of argocd-notifications-cm configmap:
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.slack: |
  7. token: $slack-token
  1. Add annotation in application yaml file to enable notifications for specific argocd app
  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Application
  3. metadata:
  4. annotations:
  5. notifications.argoproj.io/subscribe.on-sync-succeeded.slack: my_channel
  1. Annotation with more than one trigger multiple of destinations and recipients
  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Application
  3. metadata:
  4. annotations:
  5. notifications.argoproj.io/subscriptions: |
  6. - trigger: [on-scaling-replica-set, on-rollout-updated, on-rollout-step-completed]
  7. destinations:
  8. - service: slack
  9. recipients: [my-channel-1, my-channel-2]
  10. - service: email
  11. recipients: [recipient-1, recipient-2, recipient-3 ]
  12. - trigger: [on-rollout-aborted, on-analysis-run-failed, on-analysis-run-error]
  13. destinations:
  14. - service: slack
  15. recipients: [my-channel-21, my-channel-22]

Templates

Notification templates can be customized to leverage slack message blocks and attachments feature.

Slack - 图6

The message blocks and attachments can be specified in blocks and attachments string fields under slack field:

  1. template.app-sync-status: |
  2. message: |
  3. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  4. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  5. slack:
  6. attachments: |
  7. [{
  8. "title": "{{.app.metadata.name}}",
  9. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  10. "color": "#18be52",
  11. "fields": [{
  12. "title": "Sync Status",
  13. "value": "{{.app.status.sync.status}}",
  14. "short": true
  15. }, {
  16. "title": "Repository",
  17. "value": "{{.app.spec.source.repoURL}}",
  18. "short": true
  19. }]
  20. }]

The messages can be aggregated to the slack threads by grouping key which can be specified in a groupingKey string field under slack field. groupingKey is used across each template and works independently on each slack channel. When multiple applications will be updated at the same time or frequently, the messages in slack channel can be easily read by aggregating with git commit hash, application name, etc. Furthermore, the messages can be broadcast to the channel at the specific template by notifyBroadcast field.

  1. template.app-sync-status: |
  2. message: |
  3. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  4. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  5. slack:
  6. attachments: |
  7. [{
  8. "title": "{{.app.metadata.name}}",
  9. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  10. "color": "#18be52",
  11. "fields": [{
  12. "title": "Sync Status",
  13. "value": "{{.app.status.sync.status}}",
  14. "short": true
  15. }, {
  16. "title": "Repository",
  17. "value": "{{.app.spec.source.repoURL}}",
  18. "short": true
  19. }]
  20. }]
  21. # Aggregate the messages to the thread by git commit hash
  22. groupingKey: "{{.app.status.sync.revision}}"
  23. notifyBroadcast: false
  24. template.app-sync-failed: |
  25. message: |
  26. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  27. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  28. slack:
  29. attachments: |
  30. [{
  31. "title": "{{.app.metadata.name}}",
  32. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  33. "color": "#ff0000",
  34. "fields": [{
  35. "title": "Sync Status",
  36. "value": "{{.app.status.sync.status}}",
  37. "short": true
  38. }, {
  39. "title": "Repository",
  40. "value": "{{.app.spec.source.repoURL}}",
  41. "short": true
  42. }]
  43. }]
  44. # Aggregate the messages to the thread by git commit hash
  45. groupingKey: "{{.app.status.sync.revision}}"
  46. notifyBroadcast: true

The message is sent according to the deliveryPolicy string field under the slack field. The available modes are Post (default), PostAndUpdate, and Update. The PostAndUpdate and Update settings require groupingKey to be set.